package com.aggron.concurrency.aqs;

import com.aggron.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author LR2SD
 * @description CyclicBarrier测试类
 * @date 2018-06-12 21:04
 */
@ThreadSafe
@Slf4j
public class CyclicBarrierExample1 {
    public static final int THREAD_COUNT=10;

    private static CyclicBarrier cyclicBarrier=new CyclicBarrier(5);

    public static void main(String [] args) throws Exception {

        ExecutorService executorService= Executors.newCachedThreadPool();
        for (int i = 0; i < THREAD_COUNT; i++) {
            final int  threadNum=i;
            Thread.sleep(1000);
            executorService.execute(()->{
                try {
                    race(threadNum);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {

                }
            });
        }

        executorService.shutdown();
    }

    private static void race(int threadNum) throws Exception {
        Thread.sleep(1000);

        log.info("{} is ready",threadNum);
        cyclicBarrier.await();
        log.info("{} is continue",threadNum );

    }
}
